﻿using System;
using System.Windows;
using System.Windows.Input;

namespace Gmantis.Controls
{
    public class TapHelper
    {
        // Fields
        private UIElement _element;
        private bool _handledEventsToo;
        private Point _lastPos;
        private DateTime _lastTime;
        private const double DoubleClickDistance = 4.0;
        private const int DoubleClickTime = 400;

        // Events
        public event EventHandler<TappedEventArgs> DoubleTapped;
        public event EventHandler<TappedEventArgs> Tapped;

        // Methods
        public TapHelper(UIElement element, bool handledEventsToo = false)
        {
            this._element = element;
            this._handledEventsToo = handledEventsToo;
            this.InitializeClick();
            this.InitializeDoubleClick();
        }

        private void DoubleTapPointerDown(object sender, RoutedEventArgs e)
        {
            if (this.DoubleTapped != null)
            {
                DateTime now = DateTime.Now;
                Point position = InputEventArgs.GetPosition(e, this._element);
                if (now.Subtract(this._lastTime).TotalMilliseconds <= 400.0)
                {
                    double num = this._lastPos.X - position.X;
                    double num2 = this._lastPos.Y - position.Y;
                    if (((num * num) + (num2 * num2)) <= 4.0)
                    {
                        this.DoubleTapped(this._element, new TappedEventArgs(e));
                        this._lastPos = new Point();
                        this._lastTime = new DateTime();
                        return;
                    }
                }
                this._lastTime = now;
                this._lastPos = position;
            }
        }

        private void FinalizeClick()
        {
            this._element.RemoveHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.TapPointerDown));
            this._element.RemoveHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.TapPointerUp));
        }

        private void FinalizeDoubleClick()
        {
            this._element.RemoveHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.DoubleTapPointerDown));
        }

        private void InitializeClick()
        {
            this._lastPos = new Point();
            this._element.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.TapPointerDown), true);
            this._element.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.TapPointerUp), this._handledEventsToo);
        }

        private void InitializeDoubleClick()
        {
            this._lastPos = new Point();
            this._lastTime = new DateTime();
            this._element.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.DoubleTapPointerDown), this._handledEventsToo);
        }

        private void TapPointerDown(object sender, RoutedEventArgs e)
        {
            if (InputEventArgs.GetIsHandled(e) && !this._handledEventsToo)
            {
                this._lastPos = new Point();
            }
            else
            {
                this._lastPos = InputEventArgs.GetPosition(e, this._element);
            }
        }

        private void TapPointerUp(object sender, RoutedEventArgs e)
        {
            if (this.Tapped != null)
            {
                DateTime now = DateTime.Now;
                Point position = InputEventArgs.GetPosition(e, this._element);
                double num = this._lastPos.X - position.X;
                double num2 = this._lastPos.Y - position.Y;
                if (((num * num) + (num2 * num2)) <= 4.0)
                {
                    this.Tapped(this._element, new TappedEventArgs(e));
                }
            }
        }
    }
}
